package com.trivia.fortune.sdk;

import static com.trivia.fortune.sdk.TrFBusiness.GAME_MAIN_CLS;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.applovin.sdk.AppLovinMediationProvider;
import com.applovin.sdk.AppLovinPrivacySettings;
import com.applovin.sdk.AppLovinSdk;
import com.applovin.sdk.AppLovinSdkConfiguration;
import com.applovin.sdk.AppLovinSdkInitializationConfiguration;
import com.applovin.sdk.AppLovinSdkSettings;
import com.trivia.fortune.sdk.ad.TrFBigoRewardVideo;
import com.trivia.fortune.sdk.ad.TrFMaxReward;
import com.trivia.fortune.sdk.utils.TrFUtils;

import sg.bigo.ads.BigoAdSdk;
import sg.bigo.ads.api.AdConfig;

public class TrFSDKManager {

    public static TrFSDKManager getInstance() {
        return InstanceHolder.GAME_APPLICATION;
    }

    private static class InstanceHolder {
        private static final TrFSDKManager GAME_APPLICATION = new TrFSDKManager();
    }


    public void init(Activity activity) {
        Log.e("TriviaW", "onCreateonCreate");
        Context mApplication = activity.getApplicationContext();

        registerActivityLifecycleCallbacks(mApplication);

        TrFBusiness.init(mApplication);

        //AppLovinPrivacySettings
        AppLovinPrivacySettings.setHasUserConsent(true, mApplication);
        AppLovinPrivacySettings.setDoNotSell(false, mApplication);
        //AppLovinSdkSettings
        AppLovinSdkSettings settings = AppLovinSdk.getInstance(mApplication).getSettings();
        //settings.setVerboseLogging(true);
        settings.setExtraParameter("enable_black_screen_fixes", "true");
//        AppLovinSdk.getInstance( context ).getSettings().setVerboseLogging( true );
        // Create the initialization configuration
        AppLovinSdkInitializationConfiguration initConfig = AppLovinSdkInitializationConfiguration.builder(
                        TrFUtils.decrypt(TrFConstant.MAX_KEY), mApplication)
                .setMediationProvider(AppLovinMediationProvider.MAX)
                .build();
        // Initialize the SDK with the configuration
        AppLovinSdk.getInstance(mApplication).initialize(initConfig, new AppLovinSdk.SdkInitializationListener() {
            @Override
            public void onSdkInitialized(final AppLovinSdkConfiguration config) {
//                AppLovinSdk.getInstance(context).showMediationDebugger();
                Log.e("TriviaW", "onSdkInitialized");
                TrFMaxReward.getInstance().loadRewardAd(mApplication);
            }
        });

        //bigo
        AdConfig config = new AdConfig.Builder()
                .setAppId(TrFConstant.BIGO_APP_ID)
                .build();
        BigoAdSdk.initialize(activity, config, new BigoAdSdk.InitListener() {
            @Override
            public void onInitialized() {
                TrFBigoRewardVideo.getInstance().loadRewardAd();
            }
        });
    }

    private void registerActivityLifecycleCallbacks(Context mApplication) {
        ((Application) mApplication).registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
                TrFBusiness.onActivityCreate(activity);
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {
                try{
                    if (activity.getClass().getName().equals(GAME_MAIN_CLS)) {
                        TrFBigoRewardVideo.getInstance().onDestroy();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }
}
